//
//  SNProductionPlanningCell.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/23.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNProductionEmptyPlanningCell.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
@interface SNProductionEmptyPlanningCell()
/** imageView */
@property (weak, nonatomic) UIView * bgView;
@property (weak, nonatomic) IBOutlet UILabel *typenameLabel;
@property (weak, nonatomic) IBOutlet UILabel *usernameLabel;
@property (weak, nonatomic) IBOutlet UILabel *userphoneLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@end
@implementation SNProductionEmptyPlanningCell
-(void)setModel:(SNPlantingDecisionMakerResult *)model{
    _model = model;
    //  [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:kUserIconDefaultImage];
    NSLog(@"%@",model.photo);
    //    model.photo = @"http://zhuoxin.zxdev.cn//api/v1/base/attach/3852314630097600512/down/";
    if (model.photo.length > 0) {
        NSString * urlString = [model.photo stringByReplacingOccurrencesOfString:@"//" withString:@"/"];
        urlString = [urlString stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
        
        NSFileManager * manager = [NSFileManager defaultManager];
        NSString * filename = [SNUtils cachedFileNameForKey:urlString];
        if ([manager fileExistsAtPath:kCachePath(filename)]) {
            [self.iconImageView sd_setImageWithURL:[NSURL fileURLWithPath:kCachePath(filename)] placeholderImage:kUserIconDefaultImage];
        }else{
            [[ZXHTTPManager sharedManager] GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSData *  _Nullable responseObject) {
                
                NSString * filename = [SNUtils cachedFileNameForKey:urlString];
                
                [responseObject writeToFile:kCachePath(filename) atomically:YES];
                
                // UIImage * image = [UIImage imageWithData:responseObject];
                NSLog(@"kCachePath(filename) = %@",kCachePath(filename));
                [self.iconImageView sd_setImageWithURL:[NSURL fileURLWithPath:kCachePath(filename)] placeholderImage:kUserIconDefaultImage];
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                NSLog(@"%@",error);
            }];
        }
    }else{
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:kUserIconDefaultImage];
    }
    
    
    self.usernameLabel.text = model.custName;
    
    self.typenameLabel.text = model.fullName;
    
    self.userphoneLabel.text = [NSString stringWithFormat:@"电话: %@",model.phone];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.contentView.backgroundColor = HexRGB(0xf5f5f5);
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    //    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];
    //    view2.backgroundColor = [UIColor redColor];
    //    [self.view addSubview:view2];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        self.iconImageView.layer.cornerRadius = self.iconImageView.bounds.size.width * 0.5;
        self.iconImageView.layer.masksToBounds = YES;
    });
    
    UIView * bgView = ({
        UIView * view = [[UIView alloc] init];
        
        view.backgroundColor = [UIColor whiteColor];
        //        view.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"icon_bgimg"].CGImage);
        view.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"bgbg"].CGImage);
        // [self addSubview:view];
        // [self.view addSubview:view];
        [self.contentView insertSubview:view atIndex:0];
        
        [view mas_makeConstraints:^(MASConstraintMaker * make) {
            make.left.mas_equalTo(self.contentView.mas_left).offset(12);
            make.right.mas_equalTo(self.contentView.mas_right).offset(-12);
            make.top.mas_equalTo(self.contentView.mas_top).offset(0);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0);
        }];
        
        view.layer.cornerRadius = 4;
        view.layer.masksToBounds = YES;
        
        view;
    });
    self.bgView = bgView;
    
}



@end
